PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Date-Time Arithmetic

AppleScript supports these operations with the + and - operators on date and time difference values:

date + timeDifference
--result: date
date - date
--result: timeDifference
date - timeDifference
--result: date

where date is a date value and timeDifference is an integer value specifying a time difference in seconds.

To simplify the notation of time differences, you can also use one or more of these constants:

minutes 60
hours 60 * minutes
days 24 * hours
weeks 7 * days

Here's an example:

date "Apr 15, 1998" + 4 * days + 3 * hours + 2 * minutes

It is often useful to be able to specify a time difference between two dates; for example:

set timeInvestment to current date - date "January 22, 1998"

After running this script, the value of the timeInvestment variable is an integer that specifies the number of seconds between the two dates. If you then add this time difference to the starting date (January 22, 1998), AppleScript returns a date value equal to the current date when the timeInvestment variable was set.

To express a time difference in more convenient form, divide the number of seconds by the appropriate constant:

31449600 / weeks
--result: 52.0
62899200 / (weeks * 52)
--result (in years): 2.0
151200 / days
--result: 1.75

To get an integral number of hours, days, and so on, use the div operator:

151200 div days
--result: 1

To get the difference, in seconds, between the current time and Greenwich mean time, use the scripting addition command Time to GMT. For example, if you are in Cupertino, California, and your computer is set to Pacific Standard Time, Time to GMT produces this result:

time to GMT
--result: -28800

For more information about the Time to GMT command, and about the other standard scripting addition commands distributed with AppleScript, see the following website:

www.apple.com/applescript

For more information on working with dates, see Date.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)